package edu.biwu;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * ServletContext中的方法
 *   String getRealPath():传入一个类的路径,然后返回一个资源的真实路径
 *
 */
@WebServlet("/context01")
public class ServletContextDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = getServletContext();
//       System.out.println(servletContext);
//        String realPath = servletContext.getRealPath("/WEB-INF/classes/db.properties");
//      System.out.println(realPath);
//        Properties properties = new Properties();
//        properties.load(new FileInputStream(realPath));
//        System.out.println(properties);
        InputStream resourceAsStream = servletContext.getResourceAsStream("/WEB-INF/classes/db.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        System.out.println(properties);


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
